package org.yxjme.zxinglib;

import android.Manifest;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.jbh.permissionlibrary.PermissionManager;
import com.jbh.permissionlibrary.base.BasePermissionActivity;
import com.jbh.permissionlibrary.i.CheckPermissionListener;
import com.jbh.permissionlibrary.i.PermissionRequestCode;
import com.jbh.zxinglibrary.CommonScanActivity;

import org.yxjme.ijkplayerlibrary.android.YXJControlVideoView;
import org.yxjme.ijkplayerlibrary.android.YXJVideoView;

public class MainActivity extends BasePermissionActivity {

    String videoUrl="http://playertest.longtailvideo.com/adaptive/wowzaid3/playlist.m3u8";
    String videoUrl1="https://taojinapp.oss-cn-hangzhou.aliyuncs.com/ProductImage/2111/22/2111221426410151.mp4";


    /*暂未图*/
    String videoUrl2="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1115%2F092221102018%2F210922102018-8-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1641984227&t=1cf9485dffaf2d487754b04a2c23f893";

    YXJControlVideoView controlVideoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        registerReceiverSaoMa();



        YXJVideoView mYXJVideoVIew = findViewById(R.id.mYXJVideoVIew);
         controlVideoView = new YXJControlVideoView(this);
        controlVideoView
                //是否支持底部控制栏 默认支持
                .isSupportBottomControl(true)
                //静音
                .isSupportVocClose(false)
                //默认显示占位图 不然是一坨黑
                .setPlaceHolder(videoUrl2)
                //支持单击视频
                .setOnVideoClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    }
                })
                //支持长按视频
                .setOnVideoLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        return false;
                    }
                });
        mYXJVideoVIew.addControlView(controlVideoView);
        mYXJVideoVIew.setDataPath(videoUrl1, YXJVideoView.VideoType.URL);
    }


    public void saoMa(View view){
        PermissionManager.newInstance().checkPermission(this, Manifest.permission.CAMERA,
                PermissionRequestCode.CAMERA_REQUEST_CODE, new CheckPermissionListener() {
                    @Override
                    public void onNoNeedCheckPermission() {
                        startActivity(new Intent(MainActivity.this, CommonScanActivity.class)
                        .putExtra(CommonScanActivity.RESULT_KEY,"aaa"));
                    }
                });
    }


    @Override
    protected void onAgreePermission(int requestCode) {
        super.onAgreePermission(requestCode);
        switch (requestCode){
            case PermissionRequestCode.CAMERA_REQUEST_CODE:
                startActivity(new Intent(this, CommonScanActivity.class));
                break;
        }
    }


    private SaoMaBroadcastReceiver saoMaBroadcastReceiver ;
    public void registerReceiverSaoMa(){
        saoMaBroadcastReceiver = new SaoMaBroadcastReceiver();
        IntentFilter intentFilter=new IntentFilter(CommonScanActivity.ACTION);
        registerReceiver(saoMaBroadcastReceiver,intentFilter);
    }



    public void unregisterReceiverSaoMa(){
        if (saoMaBroadcastReceiver!=null)
        unregisterReceiver(saoMaBroadcastReceiver);
    }


    @Override
    protected void onResume() {
        super.onResume();
        controlVideoView.onResume();
    }



    @Override
    protected void onPause() {
        super.onPause();
        controlVideoView.onPause();
    }




    @Override
    protected void onDestroy() {
        super.onDestroy();
        controlVideoView.destroy();
        unregisterReceiverSaoMa();
    }
}